package com.soccertgc.data.filters
{
	public class DataFilters
	{
		
		// -------------------------------------------------------------------------------
		// FIELDS
		// -------------------------------------------------------------------------------
		
		private var _filters:Vector.<DataFilter>;
		
		// -------------------------------------------------------------------------------
		// CONSTRUCTOR
		// -------------------------------------------------------------------------------	
		
		public function DataFilters()
		{
			_filters = new Vector.<DataFilter>();
		}
		
		// -------------------------------------------------------------------------------
		// PROPERTIES
		// -------------------------------------------------------------------------------
		
		public function get count():int
		{
			return _filters.length;
		}
		
		public function get hasFilters():Boolean
		{
			return _filters.length > 0;
		}
		
		
		// -------------------------------------------------------------------------------
		// PRIVATE METHODS
		// -------------------------------------------------------------------------------
		
		public function indexOf(name:String):int
		{
			for (var i:int = 0; i < _filters.length; i++)
			{
				if (_filters[i].name == name) return i;
			}
			return -1;
		}
		
		// -------------------------------------------------------------------------------
		// PUBLIC METHODS
		// -------------------------------------------------------------------------------
		
		public function get(name:String):DataFilter
		{
			var index:int = indexOf(name);
			if (index > -1) return _filters[index];
			return null;
		}
		
		public function contains(filter:DataFilter):Boolean
		{
			return indexOf(filter.name) > -1;
		}
		
		public function add(filter:DataFilter):void
		{
			var index:int = indexOf(filter.name);
			if (index == -1)
			{
				_filters.push(filter);
			}
			else
			{
				_filters[index].value = filter.value;
			}
		}
		
		public function remove(filter:DataFilter):void
		{
			var index:int = indexOf(filter.name);
			if (index > -1)
			{
				_filters.splice(index, 1);
			}
		}
		
		public function removeByName(name:String):void
		{
			var index:int = indexOf(name);
			if (index > -1)
			{
				_filters.splice(index, 1);
			}
		}
		
		public function clear():void
		{
			_filters.length = 0;
		}
		
		public function evaluate(obj:Object):Boolean
		{
			for (var i:int = 0; i < _filters.length; i++)
			{
				if (!_filters[i].evaluate(obj)) return false;
			}
			return true;
		}
		
	}
}